<template>
  <div class="login-conter">
    <div class="login-box">
      <div class="avatar-box">
        <img src="../assets/1 (2).jpg" alt>
      </div>
      <el-form
        label-width="0px"
        class="login_from"
        :model="loginFrom"
        :rules="loginFromRules"
        ref="loginFromRef"
      >
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginFrom.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="loginFrom.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //这是登录表单的绑定对象
      loginFrom: {
        username: '',
        password: ''
      },
      //这是登录表单的验证规则对象
      loginFromRules: {
        //这是用户名的验证
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        //这是密码的验证
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //重置的点击事件函数
    resetFrom() {
      this.$refs.loginFromRef.resetFields()
    },
    //登录的点击事件函数
    login() {
      this.$refs.loginFromRef.validate(async boolean => {
        if (!boolean) return
        var { data: res } = await this.$http.post('login', this.loginFrom)
        if (res.meta.status != 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        console.log(res)
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-conter {
  background: #007acc;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 300px;
  background: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar-box {
    width: 130px;
    height: 130px;
    border: 1px solid #ddd;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
    box-shadow: 0, 0, 10px, #ddd;
  }
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}
.login_from {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
  .btns {
    display: flex;
    justify-content: center;
  }
}
</style>
